package com.lc.advertising.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.request.EditPasswordApi;
import com.lc.advertising.http.response.RegisterBean;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.util.SPUtil;
import com.lc.advertising.widget.PasswordEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditPwdActivity extends AppActivity {
    @BindView(R.id.et_register_password1)
    PasswordEditText etRegisterPassword1;
    @BindView(R.id.ll_password1)
    LinearLayout llPassword1;
    @BindView(R.id.et_register_password2)
    PasswordEditText etRegisterPassword2;
    @BindView(R.id.ll_password2)
    LinearLayout llPassword2;
    @BindView(R.id.btn_login_commit)
    Button btnLoginCommit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_editpwd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        etRegisterPassword1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {

                    llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                    llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);

                }
            }
        });
        etRegisterPassword2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {

                    llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                    llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);

                }
            }
        });

    }

    @OnClick({R.id.image_back, R.id.ll_password1, R.id.ll_password2, R.id.btn_login_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.ll_password1:
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                break;
            case R.id.ll_password2:
                llPassword1.setBackgroundResource(R.drawable.xui_config_color_edittext_disable);
                llPassword2.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
                break;
            case R.id.btn_login_commit:
                if (!etRegisterPassword1.getText().toString().trim().equals(etRegisterPassword2.getText().toString().trim())){
                    toast("两次输入密码不一致");
                }
                edit_password();
                break;
        }
    }
    private void edit_password() {
        if (!etRegisterPassword1.getText().toString().trim().equals(etRegisterPassword2.getText().toString().trim())) {
            toast("两次输入密码不一致");
        }

        EasyHttp.post(this)
                .api(new EditPasswordApi().setUid(SPUtil.get(this, IntentKey.UID, ""))

                        .setPassword(etRegisterPassword1.getText().toString())
                        .setPassword2(etRegisterPassword2.getText().toString()))
                .request(new HttpCallback<RegisterBean>(this) {


                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMessage());
                        if (data.getCode() == 200) {
                            finish();
                        }
                    }


                });
    }
}
